//
//  LJMainNavigationController.swift
//  LJWeibo
//
//  Created by 连俊杨 on 2017/10/6.
//  Copyright © 2017年 yang_ljun. All rights reserved.
//

import UIKit

class LJMainNavigationController: UINavigationController {

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        /// 当不是栈底控制器的时候，不需要隐藏
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            /// 所有的控制器都必须继承基类控制器
            if viewController is LJBaseViewController {
                
                var title = "返回"
                
                if childViewControllers.count == 1 {
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                /// 目标控制器的左导航按钮做为返回按钮
                let leftBtn = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack: true)
                (viewController as! LJBaseViewController).lj_navigationItem.leftBarButtonItem = leftBtn
            }
            
        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    /// MARK: 返回事件
    @objc fileprivate func popToParent() {
        popViewController(animated: true)
    }
}









